const RedBlackTree = require("../template")

function testRedBlackTreeBasic() {
  const rbt = new RedBlackTree()

  console.log(
    "1. 基本插入测试:",
    (() => {
      rbt.insert(10)
      rbt.insert(20)
      rbt.insert(30)
      rbt.insert(40)
      rbt.insert(50)
      return rbt.search(10) !== null &&
             rbt.search(20) !== null &&
             rbt.search(30) !== null &&
             rbt.search(40) !== null &&
             rbt.search(50) !== null
    })()
  )

  console.log(
    "2. 基本搜索测试:",
    (() => {
      return rbt.search(10) !== null &&
             rbt.search(20) !== null &&
             rbt.search(60) === null &&
             rbt.search(5) === null
    })()
  )

  console.log(
    "3. 红黑树性质测试:",
    (() => {
      return rbt.isValid() === true
    })()
  )

  console.log(
    "4. 中序遍历测试:",
    (() => {
      const result = rbt.inorder()
      const expected = [10, 20, 30, 40, 50]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 最小值和最大值测试:",
    (() => {
      return rbt.getMin() === 10 &&
             rbt.getMax() === 50
    })()
  )

  console.log(
    "6. 基本删除测试:",
    (() => {
      rbt.delete(30)
      return rbt.search(30) === null &&
             rbt.search(10) !== null &&
             rbt.search(20) !== null &&
             rbt.search(40) !== null &&
             rbt.search(50) !== null
    })()
  )

  console.log(
    "7. this上下文红黑树测试:",
    (() => {
      const rbtObj = {
        multiplier: 2,
        processRedBlackTree: function(values) {
          const rbt = new RedBlackTree()
          values.forEach(val => rbt.insert(val))
          return rbt.getSize() * this.multiplier
        }
      }
      return rbtObj.processRedBlackTree([1, 2, 3]) === 6 // 3*2=6
    })()
  )

  console.log(
    "8. 复杂对象this绑定红黑树测试:",
    (() => {
      const rbtConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(values) {
          const rbt = new RedBlackTree()
          values.forEach(val => rbt.insert(val))
          return rbt.getSize() >= this.config.threshold
        }
      }
      return rbtConfigObj.processWithConfig([1, 2, 3]) === true
    })()
  )

  console.log(
    "9. 红黑树一致性测试:",
    (() => {
      const consistencyRbt = new RedBlackTree()
      consistencyRbt.insert(5)
      consistencyRbt.insert(3)
      consistencyRbt.insert(7)
      const size1 = consistencyRbt.getSize()
      consistencyRbt.delete(3)
      const size2 = consistencyRbt.getSize()
      return size1 === 3 && size2 === 2
    })()
  )

  console.log(
    "10. 红黑树边界测试:",
    (() => {
      const boundaryRbt = new RedBlackTree()
      boundaryRbt.insert(1)
      return boundaryRbt.getMin() === 1 &&
             boundaryRbt.getMax() === 1 &&
             boundaryRbt.getSize() === 1
    })()
  )

  console.log(
    "11. 红黑树性能测试:",
    (() => {
      const perfRbt = new RedBlackTree()
      const values = Array.from({ length: 100 }, (_, i) => i + 1)
      const start = performance.now()
      values.forEach(val => perfRbt.insert(val))
      values.forEach(val => perfRbt.search(val))
      const end = performance.now()
      return perfRbt.getSize() === 100 && (end - start) < 1000
    })()
  )

  console.log(
    "12. 红黑树复杂测试:",
    (() => {
      const complexRbt = new RedBlackTree()
      const values = [50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85]
      values.forEach(val => complexRbt.insert(val))
      const isValid = complexRbt.isValid()
      const inorder = complexRbt.inorder()
      const min = complexRbt.getMin()
      const max = complexRbt.getMax()
      return isValid === true &&
             inorder.length === 15 &&
             min === 10 &&
             max === 85
    })()
  )
}

testRedBlackTreeBasic()
